#include "Budget.h"
#include "BudgetPlanner.h"
#include "Date.h"
#include "Expense.h"
#include "ExpenseTracker.h"
#include "Priority.h"
#include "Profile.h"
#include "User.h"
#include "Category.h"
#include "CategoryList.h"
#include "Log.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include <sstream>

using namespace std;

// functions separated(just a thought)
// will take in parameters

void search(int typeOfExpenses, int typeOfSearch, string keyword, CategoryList overallCategory)
{
	int entryNum = 0;

	if((typeOfExpenses>= 1) && (typeOfExpenses <= 7)){
		vector<Log>  list = overallCategory.allCategory[typeOfExpenses-1].logList;
		if(typeOfSearch==1)//date
		{
			//condition 1: for cases 1-7 (to search within the category)        
			for(unsigned int i=0; i<list.size(); i++)
			{
				std::stringstream ss;
				string s;
				ss << (list.at(i)).getAmount();
				s = ss.str();
				if(keyword == (list.at(i)).getDate().getCombination())
				{
					entryNum ++;
					cout << "Entry number: " << entryNum << endl;
					cout << "Date: " << (list.at(i)).getDate().getDay() << 
						" "<<	(list.at(i)).getDate().getMonth() <<
						" "<<	(list.at(i)).getDate().getYear()<< endl;
					cout << "Type of expenses: " << typeOfExpenses << endl;
					cout << "Amount: " << (list.at(i)).getAmount() << endl;
					cout << "Comments: " << (list.at(i)).getComment() << endl;
				}
			}
		}

		else if(typeOfSearch==2)//price
		{
			for(unsigned int i=0; i<list.size(); i++)
			{  
				std::stringstream ss;
				string s;
				ss << (list.at(i)).getAmount();
				s = ss.str();

				if(keyword == s)
				{
					entryNum ++;
					cout << "Entry number: " << entryNum << endl;
					cout << "Date: " << (list.at(i)).getDate().getDay() << 
						" "<<	(list.at(i)).getDate().getMonth() <<
						" "<<	(list.at(i)).getDate().getYear()<< endl;
					cout << "Type of expenses: " << typeOfExpenses << endl;
					cout << "Amount: " << (list.at(i)).getAmount() << endl;
					cout << "Comments: " << (list.at(i)).getComment() << endl;
				}

			}
		}
		else if(typeOfSearch==3)//comment
		{
			for(unsigned int i=0; i<list.size(); i++)
			{


				if((list.at(i)).getComment().find(keyword) != string::npos)
				{
					entryNum ++;
					cout << "Entry number: " << entryNum << endl;
					cout << "Date: " << (list.at(i)).getDate().getDay() << 
						" "<<	(list.at(i)).getDate().getMonth() <<
						" "<<	(list.at(i)).getDate().getYear()<< endl;
					cout << "Type of expenses: " << typeOfExpenses << endl;
					cout << "Amount: " << (list.at(i)).getAmount() << endl;
					cout << "Comments: " << (list.at(i)).getComment() << endl;
				}

			}
		}
		else
		{
			cout << "invalid search" << endl;
		}
		system("PAUSE");
	}

	else if (typeOfSearch == 8){
		for (size_t i = 0; i < overallCategory.allCategory.size(); i++){
			vector<Log>  list = overallCategory.allCategory[typeOfExpenses-1].logList;
			if(typeOfSearch==1)//date
			{// || (typeOfExpenses==8)){

				//condition 1: for cases 1-7 (to search within the category)

				for(unsigned int i=0; i<list.size(); i++)
				{


					std::stringstream ss;
					string s;
					ss << (list.at(i)).getAmount();
					s = ss.str();
					if(keyword == (list.at(i)).getDate().getCombination())
					{
						entryNum ++;
						cout << "Entry number: " << entryNum << endl;
						cout << "Date: " << (list.at(i)).getDate().getDay() << 
							" "<<	(list.at(i)).getDate().getMonth() <<
							" "<<	(list.at(i)).getDate().getYear()<< endl;
						cout << "Type of expenses: " << typeOfExpenses << endl;
						cout << "Amount: " << (list.at(i)).getAmount() << endl;
						cout << "Comments: " << (list.at(i)).getComment() << endl;
					}
				}
			}

			else if(typeOfSearch==2)//price
			{
				for(unsigned int i=0; i<list.size(); i++)
				{

					std::stringstream ss;
					string s;
					ss << (list.at(i)).getAmount();
					s = ss.str();

					if(keyword == s)
					{
						entryNum ++;
						cout << "Entry number: " << entryNum << endl;
						cout << "Date: " << (list.at(i)).getDate().getDay() << 
							" "<<	(list.at(i)).getDate().getMonth() <<
							" "<<	(list.at(i)).getDate().getYear()<< endl;
						cout << "Type of expenses: " << typeOfExpenses << endl;
						cout << "Amount: " << (list.at(i)).getAmount() << endl;
						cout << "Comments: " << (list.at(i)).getComment() << endl;
					}

				}
			}
			else if(typeOfSearch==3)//comment
			{
				for(unsigned int i=0; i<list.size(); i++)
				{
					cout << "stage 1" << endl;


					if((list.at(i)).getComment().find(keyword) != string::npos)
					{
						cout << "stage 2" << endl;
						entryNum ++;
						cout << "Entry number: " << entryNum << endl;
						cout << "Date: " << (list.at(i)).getDate().getDay() << 
							" "<<	(list.at(i)).getDate().getMonth() <<
							" "<<	(list.at(i)).getDate().getYear()<< endl;
						cout << "Type of expenses: " << typeOfExpenses << endl;
						cout << "Amount: " << (list.at(i)).getAmount() << endl;
						cout << "Comments: " << (list.at(i)).getComment() << endl;
					}
				}

			}
			else
			{
				cout << "invalid search" << endl;
			}




		}
		system("PAUSE");
	}
	else{
		cout << "invalid search" << endl;
		system("PAUSE");
	}
}




void alerts(){
	//display notifications, simple as that
}



int main(){
	int selection = 99;
	CategoryList overallCategory;
	int choice1;
	cin >> choice1;
	if(choice1 == 1)
		overallCategory.newFile();
	overallCategory.fileToVector();

	/*
	overallCategory.allCategory[0].logList[0].setAmount(100);
	overallCategory.allCategory[0].logList[0].setDate(d);
	overallCategory.allCategory[0].logList[0].setComment("Testing");
	*/

	while(selection != 4)
	{
		system("CLS");
		cout << "Welcome! Please make a selection: "<< endl;
		cout << "1 File 2 Tools 3 Help 4 Quit" << endl;
		cin >> selection;

		if (selection == 1){
			int newselection;
			while(1)
			{
				system("CLS");
				cout << "Please make a selection: "<< endl;
				cout << "1 Open 2 New Profile 3 Exit" << endl;
				cin >> newselection;
				if (newselection == 1){
					overallCategory.defaultScr();
					//	alerts();
					// would preferably take the name of the txtfile
				}
				else if (newselection == 2){
					//create new profile
					//write to txtfile
					User user1;

					string name;
					string country;
					double budget;
					cout << "Enter name: " << endl;
					cin >> name;
					cout << "Enter country: " << endl;
					cin >> country;

					user1.setProfile(name, country);
					//user1.getExpenseTracker

					user1.getExpenseTracker().getBudgetPlanner().setdetails(0);

					user1.getProfile().makeNewProfile();

					//alerts();
					//functions();
				}
				else if (newselection == 3){
					break;
					return 0;
				}

				

				else{
					cout << "Error!" << endl;
				}
			}
		}
		else if (selection == 2){ // search
			int typeOfExpenses, typeOfSearch;
			string keyword;
			cout <<  "What type of expense:" << endl;
			cout <<  "1 Accomodation" << endl;
			cout <<  "2 Academics" << endl;
			cout <<  "3 Debts" << endl;
			cout <<  "4 Food" << endl;
			cout <<  "5 Recreation" << endl;
			cout <<  "6 Transportation" << endl;
			cout <<  "7 Others" << endl;
			cout <<  "8 Overall" << endl;
			cin >> typeOfExpenses;

			cout << "What type of search: " << endl;
			cout <<  "1 By Date" << endl;
			cout <<  "2 By Price" << endl;
			cout <<  "3 By Comment" << endl;
			cin >> typeOfSearch;

			cout << "Search Keyword: " << endl;
			cin >> keyword;

			search(typeOfExpenses, typeOfSearch, keyword, overallCategory);


		}
		else if (selection == 3){
			//display user manual
		}

		else if ( selection == 4){

		}
		else {
			cout << "Error!" << endl;
		}	

		overallCategory.vectorToFile();
	}
	return 0;

}
